package com.inputabc.qrcoderv1.ui.mainform.component;

import com.inputabc.EzGUIFramework.util.alias.Ez;
import com.inputabc.qrcoderv1.util.CacheConfig;
import lombok.Data;
import sun.misc.Cache;

import javax.swing.*;
import java.awt.*;

/**
 * 设置面板
 *
 * @author gaoweiyi
 */
@Data
public class SettingPanel extends JPanel {
    /**
     * 设置字符集的下拉选
     */
    private JComboBox<String> charsetComboBox;
    private JLabel charsetComboBoxLabel;
    /**
     * 设置二维码图片格式的下拉选
     */
    private JComboBox<String> formatComboBox;
    private JLabel formatComboBoxLabel;
    /**
     * 设置二维码大小的微调框
     */
    private JSpinner qrcodeSizeSpinner;
    private JLabel qrcodeSizeSpinnerLabel;
    /**
     * 插入logo的按钮
     */
    private JButton insertLogoButton;
    /**
     * 是否压缩logo的多选框
     */
    private JCheckBox compressLogoCheckBox;
    /**
     * 压缩后的logo的宽度
     */
    private JSpinner compressedWidthSpinner;
    private JLabel compressedWidthSpinnerLabel;
    /**
     * 压缩后的logo的高度
     */
    private JSpinner compressedHeightSpinner;
    private JLabel compressedHeightSpinnerLabel;
    /**
     * 填写二维码内容的滚动面板
     */
    private JScrollPane contentScrollPane;
    /**
     * 填写二维码的文本区域
     */
    private JTextArea contentTextArea;
    /**
     * 生成二维码的按钮
     */
    private JButton makeButton;
    /**
     * 内容区域的弹出菜单
     */
    private JPopupMenu contentAreaPopupMenu;
    /**
     * 剪切菜单项
     */
    private JMenuItem cutMenu;
    /**
     * 复制菜单项
     */
    private JMenuItem copyMenu;
    /**
     * 粘贴菜单项
     */
    private JMenuItem pasteMenu;
    /**
     * 取消Logo按钮
     */
    private JButton cancelLogoButton;
    /**
     * 重置二维码
     */
    private JButton resetButton;

    public SettingPanel() {
        init();
    }

    public void init() {
        setLayout(null);

        int relativeX = 20;
        int relativeY = 20;
        int relativeWidth = 122;
        int relativeHeight = 26;

        charsetComboBoxLabel = new JLabel("二维码内容字符集");
        charsetComboBoxLabel.setName("charsetComboBoxLabel");
        charsetComboBoxLabel.setBounds(relativeX, relativeY, relativeWidth, relativeHeight);
        add(charsetComboBoxLabel);

        String[] charsets = CacheConfig.charsets;
        charsetComboBox = new JComboBox<>(charsets);
        charsetComboBox.setName("charsetComboBox");
        charsetComboBox.getModel().setSelectedItem(charsets[0]);
        charsetComboBox.setBounds(relativeX + relativeWidth + 5
                , relativeY, 110, 26);
        add(charsetComboBox);

        formatComboBoxLabel = new JLabel("二维码图片格式");
        formatComboBoxLabel.setName("formatComboBoxLabel");
        formatComboBoxLabel.setBounds(relativeX
                , relativeY + relativeHeight + 10,
                relativeWidth, relativeHeight);
        add(formatComboBoxLabel);

        String[] formats = CacheConfig.formats;
        formatComboBox = new JComboBox<>(formats);
        formatComboBox.setName("formatComboBox");
        formatComboBox.getModel().setSelectedItem(formats[1]);
        formatComboBox.setBounds(charsetComboBox.getX()
                , relativeY + relativeHeight + 10,
                charsetComboBox.getWidth(), charsetComboBox.getHeight());
        add(formatComboBox);

        qrcodeSizeSpinnerLabel = new JLabel("二维码大小(像素)");
        qrcodeSizeSpinnerLabel.setName("qrcodeSizeSpinnerLabel");
        qrcodeSizeSpinnerLabel.setBounds(relativeX
                , formatComboBoxLabel.getY() + relativeHeight + 10
                , relativeWidth, relativeHeight);
        add(qrcodeSizeSpinnerLabel);

        qrcodeSizeSpinner = new JSpinner();
        qrcodeSizeSpinner.setName("qrcodeSizeSpinner");
        qrcodeSizeSpinner.setEditor(new JSpinner.NumberEditor(qrcodeSizeSpinner, "#"));
        SpinnerNumberModel qrcodeSizeSpinnerModel = (SpinnerNumberModel) qrcodeSizeSpinner.getModel();
        qrcodeSizeSpinnerModel.setStepSize(10);
        qrcodeSizeSpinnerModel.setMaximum(2000);
        qrcodeSizeSpinnerModel.setMinimum(50);
        qrcodeSizeSpinnerModel.setValue(CacheConfig.defaultQRCodeSize);
        qrcodeSizeSpinner.setBounds(formatComboBox.getX()
                , formatComboBox.getY() + formatComboBox.getHeight() + 10
                , charsetComboBox.getWidth(), charsetComboBox.getHeight());
        add(qrcodeSizeSpinner);

        insertLogoButton = new JButton("插入Logo");
        insertLogoButton.setName("insertLogoButton");
        insertLogoButton.setBounds(relativeX
                , qrcodeSizeSpinnerLabel.getY() + qrcodeSizeSpinnerLabel.getHeight() + 10
                , relativeWidth, relativeHeight + 10);
        add(insertLogoButton);

        compressLogoCheckBox = new JCheckBox("伸缩Logo");
        compressLogoCheckBox.setName("compressLogoCheckBox");
        compressLogoCheckBox.setEnabled(false);
        compressLogoCheckBox.setBounds(charsetComboBox.getX()
                , qrcodeSizeSpinner.getY() + qrcodeSizeSpinner.getHeight() + 16
                , relativeWidth, relativeHeight);
        add(compressLogoCheckBox);

        compressedWidthSpinnerLabel = new JLabel("宽度(像素)");
        compressedWidthSpinnerLabel.setName("compressedWidthSpinnerLabel");
        compressedWidthSpinnerLabel.setEnabled(false);
        compressedWidthSpinnerLabel.setBounds(relativeX
                , insertLogoButton.getY() + insertLogoButton.getHeight() + 10
                , relativeWidth, relativeHeight);
        add(compressedWidthSpinnerLabel);

        compressedWidthSpinner = new JSpinner();
        compressedWidthSpinner.setName("compressedWidthSpinner");
        SpinnerNumberModel compressedWidthSpinnerModel = (SpinnerNumberModel) compressedWidthSpinner.getModel();
        compressedWidthSpinnerModel.setStepSize(1);
        compressedWidthSpinner.setEnabled(false);
        compressedWidthSpinner.setBounds(qrcodeSizeSpinner.getX()
                , insertLogoButton.getY() + insertLogoButton.getHeight() + 10
                , qrcodeSizeSpinner.getWidth(), qrcodeSizeSpinner.getHeight());
        add(compressedWidthSpinner);

        compressedHeightSpinnerLabel = new JLabel("高度(像素)");
        compressedHeightSpinnerLabel.setName("compressedHeightSpinnerLabel");
        compressedHeightSpinnerLabel.setEnabled(false);
        compressedHeightSpinnerLabel.setBounds(relativeX
                , compressedWidthSpinnerLabel.getY() + compressedWidthSpinnerLabel.getHeight() + 10
                , relativeWidth, relativeHeight);
        add(compressedHeightSpinnerLabel);

        compressedHeightSpinner = new JSpinner();
        compressedHeightSpinner.setName("compressedHeightSpinner");
        SpinnerNumberModel compressedHeightSpinnerModel = (SpinnerNumberModel) compressedHeightSpinner.getModel();
        compressedHeightSpinnerModel.setStepSize(1);
        compressedHeightSpinner.setEnabled(false);
        compressedHeightSpinner.setBounds(compressedWidthSpinner.getX()
                , compressedWidthSpinner.getY() + compressedWidthSpinner.getHeight() + 10
                , qrcodeSizeSpinner.getWidth(), qrcodeSizeSpinner.getHeight());
        add(compressedHeightSpinner);

        contentTextArea = new JTextArea();
        contentTextArea.setName("contentTextArea");
        contentTextArea.setLineWrap(true);
        contentTextArea.setWrapStyleWord(true);
        contentTextArea.setToolTipText("二维码内容");

        contentScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        contentScrollPane.setName("contentScrollPane");
        contentScrollPane.setViewportView(contentTextArea);
        contentScrollPane.setBounds(relativeX
                , compressedHeightSpinnerLabel.getY() + compressedHeightSpinnerLabel.getHeight() + 20
                , (int) (MainFrame.dimension.getWidth() / 2 - relativeX * 2)
                , 150);
        add(contentScrollPane);

        makeButton = new JButton();
        makeButton.setName("makeButton");
        makeButton.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(
                SettingPanel.class.getResource("/img/make.png")
        )));
        makeButton.setBounds(charsetComboBox.getX() + charsetComboBox.getWidth() + 20
                , relativeY + 3
                , (int) (MainFrame.dimension.getWidth() / 2 - charsetComboBox.getX() - charsetComboBox.getWidth() - 50)
                , qrcodeSizeSpinner.getY());
        makeButton.setBackground(getBackground());
        makeButton.setBorder(BorderFactory.createEmptyBorder());
        makeButton.setToolTipText("生成二维码");
        add(makeButton);

        contentAreaPopupMenu = new JPopupMenu();
        contentAreaPopupMenu.setName("contentAreaPopupMenu");
        cutMenu = new JMenuItem("剪切");
        copyMenu = new JMenuItem("复制");
        pasteMenu = new JMenuItem("粘贴");
        contentAreaPopupMenu.add(cutMenu);
        contentAreaPopupMenu.add(copyMenu);
        contentAreaPopupMenu.add(pasteMenu);
        Ez.setJPopupMenuForSwing(contentTextArea, contentAreaPopupMenu);

        cancelLogoButton = new JButton("取消Logo");
        cancelLogoButton.setName("cancelLogoButton");
        cancelLogoButton.setEnabled(false);
        cancelLogoButton.setBounds(makeButton.getX()
                , insertLogoButton.getY()
                , insertLogoButton.getWidth()
                , insertLogoButton.getHeight());
        add(cancelLogoButton);

        resetButton = new JButton("重置所有");
        resetButton.setName("resetButton");
        //
        resetButton.setBounds(makeButton.getX() + 7
                , compressedWidthSpinner.getY() + 10
                , makeButton.getWidth() - 10, makeButton.getHeight() / 2);
        add(resetButton);
    }
}
